---
--类名: Wildcard.lua
--创建日期: 2015-11-9
--创建人: 陈小虎
--功能描述: 通配字符串
--修改内容:
--修改日期:   
--修改人:     
--修改备注: 
--
local Wildcard = class("Wildcard")


function Wildcard:ctor() 
    -- local str = "我是<font r=0 g=0 b=255 size=35><sign_name></font>测试<font r=0 g=255 b=0 size=30>不会让你们#夺得</font><font r=255 g=0 b=0 size=30>布拉捏号</font>的！不会让<font r=255 g=0 b=0 size=30><sign_job></font>你们夺得<sign_sex>布拉捏号的！"
    -- -- str = "<font r=0 g=255 b=0 size=30>不会让你们#夺得</font><font r=255 g=0 b=0 size=30>布拉捏号</font>的！不会让<font r=255 g=0 b=0 size=30><sign_job></font>你们夺得<sign_sex>布拉捏号的！<font r=255 g=0 b=0 size=30>啊呀呀</font>结束了！"
    self:setTextDefaultColor(display.COLOR4_BLACK)
    -- local wordsTab = self:parseWildcardStrings(str,30,"job","sex","小小鸟")
    -- local textNode,tWidth,tHeight = self:buildTextNode(wordsTab,display.width * 10,"",outlineColor,outlineSize)
end

function Wildcard:isNeedParse(str)
    local s,e = string.find(str,"<font")
    if s == nil or e == nil then 
        return false 
    else
        return true
    end
end

function Wildcard:isNewStruct(str)
    local s,e = string.find(str,"color=")
    if s == nil or e == nil then 
        return false 
    else
        return true
    end
end

function Wildcard:replaceString(srcStr,pattern,dstStr)
    local currentString = ""
    local newLineTab = {}
    local s,e = string.find(srcStr,pattern)
    if s ~= nil then
        while s ~= nil
        do
            local frontString = string.sub(srcStr,0,s - 1)
            local behindString = string.sub(srcStr,e + 1,string.len(srcStr)) 

            currentString = currentString .. frontString .. dstStr

            s,e = string.find(behindString,pattern)
            if s == nil then
                currentString = currentString .. behindString
                break
            else
                srcStr = behindString
            end
        end
    else
        currentString = srcStr
    end

    return currentString,newLineTab
end

-------------parse wildcard strings.---------------------
function Wildcard:parseWildcardStrings(str,defaultFontSize,jobStr,sexStr,nameStr)
    self:setTextDefaultSize(defaultFontSize)
    local defaultColor = self:getTextDefaultColor()
    local patternJob = "<sign_job>"
    local patternSex = "<sign_sex>"
    local patternName = "<sign_name>"

    -- local patternTest = "<sign_test>"

    -- local s,e = string.find(str,patternJob)

    -- print("Utils.parseWildcardStrings s = ",s)
    -- print("Utils.parseWildcardStrings e = ",e)
    -- local frontString = string.sub(str,0,s - 1)
    -- local behindString = string.sub(str,e + 1,string.len(str))

    -- print("Utils.parseWildcardStrings frontString = ",frontString)
    -- print("Utils.parseWildcardStrings behindString = ",behindString)

    -- do
    --     return
    -- end


    -- print("Utils.parseWildcardStrings str = ",str)
    local testString = self:replaceString(str,patternJob,jobStr)
    testString = self:replaceString(testString,patternSex,sexStr)
    testString = self:replaceString(testString,patternName,nameStr)
    -- print("Utils.parseWildcardStrings testString = ",testString)

    local function getStringStruct(str)

        local wordsTab = {}

        local function getFontAttribute(fontFormat)
            --<font r=0 g=255 b=0 size=30>
            local r = "r="
            local g = "g="
            local b = "b="
            local size = "size="
            local space = " "
            local sEnd = ">"
            local s,e = string.find(fontFormat,r)
            local sps,spe = string.find(fontFormat,space,e)

            local rValue = defaultColor.r
            if e ~= nil and sps ~= nil then
                rValue = tonumber(string.sub(fontFormat,e + 1,sps))
            end

            s,e = string.find(fontFormat,g)
            sps,spe = string.find(fontFormat,space,e)

            local gValue = defaultColor.g
            if e ~= nil and sps ~= nil then
                gValue = tonumber(string.sub(fontFormat,e + 1,sps))
            end

            s,e = string.find(fontFormat,b)
            sps,spe = string.find(fontFormat,space,e)
            local bValue = defaultColor.b
            if e ~= nil and sps ~= nil then
                bValue = tonumber(string.sub(fontFormat,e + 1,sps))
            end

            local fontSize = defaultFontSize
            s,e = string.find(fontFormat,size)
            if e then
                sps,spe = string.find(fontFormat,space,e)
                if sps then
                    fontSize = tonumber(string.sub(fontFormat,e + 1,sps))
                else
                    sps,spe = string.find(fontFormat,sEnd,e)
                    fontSize = tonumber(string.sub(fontFormat,e + 1,sps - 1))
                end
            end

            return {r = rValue,g = gValue,b = bValue,fontSize = fontSize}
        end

        local patternFront = "<font"
        local patternFrontEnd = ">"
        local patternBehind = "</font>"

        local fs,fe = string.find(str,patternFront)
        if fs then
            if fs > 1 then
                --第一个字符串是
                local firstString = string.sub(str,1,fs - 1)
                -- print("-------------firstString = ",firstString)
                local words = {r = defaultColor.r,g = defaultColor.g,b = defaultColor.b,fontSize = defaultFontSize,content = firstString}
                table.insert(wordsTab,words)
            end

            while fs ~= nil
            do
                -- print("------------split start----------------")
                local fs1,fe1 = string.find(str,patternFrontEnd)
                local fes,fee = string.find(str,patternBehind)

                local fontString = string.sub(str,fs,fs1)
                local fontStruct = getFontAttribute(fontString)
                local structString = string.sub(str,fe1 + 1,fes - 1)
                -- print("fontStruct.r = ",fontStruct.r)
                -- print("fontStruct.g = ",fontStruct.g)
                -- print("fontStruct.b = ",fontStruct.b)
                -- print("fontStruct.fontSize = ",fontStruct.fontSize)
                -- print("structString = ",structString)

                local words = {r = fontStruct.r,g = fontStruct.g,b = fontStruct.b,fontSize = fontStruct.fontSize,content = structString}
                table.insert(wordsTab,words)

                local behindString = string.sub(str,fee + 1,string.len(str))
                str = behindString
                fs,fe = string.find(str,patternFront)
                -- print("------------split end----------------")
                if fs == nil then
                    --最后一个字符串是 behindString
                    local lastString = behindString
                    -- print("-------------lastString = ",lastString)
                    local words = {r = defaultColor.r,g = defaultColor.g,b = defaultColor.b,fontSize = defaultFontSize,content = lastString}
                    table.insert(wordsTab,words)
                    break
                else
                    if fs > 1 then
                        local middleString = string.sub(str,1,fs - 1)
                        -- print("-------------middleString = ",middleString)
                        local words = {r = defaultColor.r,g = defaultColor.g,b = defaultColor.b,fontSize = defaultFontSize,content = middleString}
                        table.insert(wordsTab,words)
                    end
                end
            end
        else
            local words = {r = defaultColor.r,g = defaultColor.g,b = defaultColor.b,fontSize = defaultFontSize,content = str}
            table.insert(wordsTab,words)
        end

        return wordsTab
    end

    local wordsTab = getStringStruct(testString)

    return wordsTab
end


-------------parse wildcard strings.---------------------
function Wildcard:parseRichTextStrings(str,defaultFontSize)
    self:setTextDefaultSize(defaultFontSize)
    local defaultColor = self:getTextDefaultColor()

    local function getStringStruct(str)

        local wordsTab = {}

        local function getFontAttribute(fontFormat)
            --<font r=0 g=255 b=0 size=30>
            local r = "r="
            local g = "g="
            local b = "b="
            local size = "size="
            local space = " "
            local sEnd = ">"
            local s,e = string.find(fontFormat,r)
            local sps,spe = string.find(fontFormat,space,e)

            local rValue = defaultColor.r
            if e ~= nil and sps ~= nil then
                rValue = tonumber(string.sub(fontFormat,e + 1,sps))
            end

            s,e = string.find(fontFormat,g)
            sps,spe = string.find(fontFormat,space,e)

            local gValue = defaultColor.g
            if e ~= nil and sps ~= nil then
                gValue = tonumber(string.sub(fontFormat,e + 1,sps))
            end

            s,e = string.find(fontFormat,b)
            sps,spe = string.find(fontFormat,space,e)
            local bValue = defaultColor.b
            if e ~= nil and sps ~= nil then
                bValue = tonumber(string.sub(fontFormat,e + 1,sps))
            end

            local fontSize = defaultFontSize
            s,e = string.find(fontFormat,size)
            if e then
                sps,spe = string.find(fontFormat,space,e)
                if sps then
                    fontSize = tonumber(string.sub(fontFormat,e + 1,sps))
                else
                    sps,spe = string.find(fontFormat,sEnd,e)
                    fontSize = tonumber(string.sub(fontFormat,e + 1,sps - 1))
                end
            end

            return {r = rValue,g = gValue,b = bValue,fontSize = fontSize}
        end

        local patternFront = "<font"
        local patternFrontEnd = ">"
        local patternBehind = "</font>"

        local fs,fe = string.find(str,patternFront)
        if fs then
            if fs > 1 then
                --第一个字符串是
                local firstString = string.sub(str,1,fs - 1)
                -- print("-------------firstString = ",firstString)
                local words = {r = defaultColor.r,g = defaultColor.g,b = defaultColor.b,fontSize = defaultFontSize,content = firstString}
                table.insert(wordsTab,words)
            end

            while fs ~= nil
            do
                -- print("------------split start----------------")
                local fs1,fe1 = string.find(str,patternFrontEnd)
                local fes,fee = string.find(str,patternBehind)

                local fontString = string.sub(str,fs,fs1)
                local fontStruct = getFontAttribute(fontString)
                local structString = string.sub(str,fe1 + 1,fes - 1)

                local words = {r = fontStruct.r,g = fontStruct.g,b = fontStruct.b,fontSize = fontStruct.fontSize,content = structString}
                table.insert(wordsTab,words)

                local behindString = string.sub(str,fee + 1,string.len(str))
                str = behindString
                fs,fe = string.find(str,patternFront)
                -- print("------------split end----------------")
                if fs == nil then
                    --最后一个字符串是 behindString
                    local lastString = behindString
                    -- print("-------------lastString = ",lastString)
                    local words = {r = defaultColor.r,g = defaultColor.g,b = defaultColor.b,fontSize = defaultFontSize,content = lastString}
                    table.insert(wordsTab,words)
                    break
                else
                    if fs > 1 then
                        local middleString = string.sub(str,1,fs - 1)
                        -- print("-------------middleString = ",middleString)
                        local words = {r = defaultColor.r,g = defaultColor.g,b = defaultColor.b,fontSize = defaultFontSize,content = middleString}
                        table.insert(wordsTab,words)
                    end
                end
            end
        else
            local words = {r = defaultColor.r,g = defaultColor.g,b = defaultColor.b,fontSize = defaultFontSize,content = str}
            table.insert(wordsTab,words)
        end

        return wordsTab
    end

    local wordsTab = getStringStruct(str)

    return wordsTab
end

function Wildcard:buildTextNodeByString(text,size,width,fontName)
    local defaults={
        KEY_FONT_FACE=fontName,
        KEY_FONT_SIZE=size,
        KEY_FONT_COLOR_STRING="#000000",
    }

    local richText = ccui.RichText:createWithXML(text,defaults)
    richText:setAnchorPoint(cc.p(0, 0))
    richText:ignoreContentAdaptWithSize(false)
    richText:setContentSize(cc.size(width, 0))

    richText:formatText()
    --richText:setOpenUrlHandler(function(url) end)
    local f_size = richText:getFormatSize()
    richText:setContentSize(f_size)
    richText:setPosition(0, - f_size.height)

    local contentLayout = ccui.Layout:create()
    contentLayout:setTouchSwallowEnabled(false)
    contentLayout:setTouchEnabled(false)
    contentLayout:setContentSize(cc.size(f_size.width,f_size.height))
    contentLayout:addChild(richText)

    return contentLayout,f_size.width,f_size.height
end

function Wildcard:buildTextNode(wordsTab,width,fontName,outlineColor,outlineSize)  
    local defaultFontSize = self:getTextDefaultSize()
    local defaultColor = self:getTextDefaultColor()
    local color = display.stringWithColor(defaultColor)

    local text = ""
    if outlineColor and outlineSize then
        local lineColor = display.stringWithColor(outlineColor)
        text = "<font size='" .. defaultFontSize .. "' color='" .. color .. "'><outline color='" .. lineColor .. "' size='" .. outlineSize .. "'>"

        for i,v in ipairs(wordsTab) do
            local fontStruct = v
            local color = display.stringWithColor(fontStruct)
            fontStruct.content = self:replaceString(fontStruct.content,"#","<br/>")
            local str = "<font size='" .. fontStruct.fontSize .. "' color='" .. color .. "'><outline color='" .. lineColor .. "' size='" .. outlineSize .. "'>" .. fontStruct.content .. "</outline></font>"
            text = text .. str
        end

        text = text .. "</outline></font>"
    else
        text = "<font size='" .. defaultFontSize .. "' color='" .. color .. "'>"

        for i,v in ipairs(wordsTab) do
            local fontStruct = v
            local color = display.stringWithColor(fontStruct)
            fontStruct.content = self:replaceString(fontStruct.content,"#","<br/>")
            local str = "<font size='" .. fontStruct.fontSize .. "' color='" .. color .. "'>" .. fontStruct.content .. "</font>"
            text = text .. str
        end

        text = text .. "</font>"
    end

    return self:buildTextNodeByString(text,defaultFontSize,width,fontName)
end


function Wildcard:setTextDefaultColor(color)
    self.mTextDefaultColor = color
end

function Wildcard:getTextDefaultColor()
    return self.mTextDefaultColor
end

function Wildcard:setTextDefaultSize(size)
    self.m_TextDefaultSize = size
end

function Wildcard:getTextDefaultSize()
    return self.m_TextDefaultSize
end

return Wildcard